<?php

namespace frontend\modules\business\controllers;

use yii;
use yii\web\Controller;
use yii\data\Pagination;

/**
 * Default controller for the `business` module
 */
class LogsController extends Controller
{
    public $business = [];
    
    public $master = null;
    
    public $logsPath = '';
    
    public function init()
    {
        parent::init();

        $this->enableCsrfValidation = false;
        
        $this->logsPath = Yii::getAlias('@module').'/business/logs/';
        
        
  
    }
    
    public function actionIndex()
    {
        $logs = scandir($this->logsPath);
        $data = [];
        $search_words = '';
        $search_files = '';

        if (Yii::$app->request->isPost) {
            
            $search_files = Yii::$app->request->post('search_files');
            $search_words = Yii::$app->request->post('search_words');
            
            $filename = $this->logsPath.$search_files;
            
            if (is_file($filename)) {
                $handle  = fopen ($filename, "r");
                
                while (!feof ($handle))
                {
                    $buffer  = fgets($handle);
                    
                    if(trim($search_words)){
                        
                        if(stripos(trim($buffer),$search_words) !== false){
                            $data[] = trim($buffer);
                        }
                    }
                }
                fclose ($handle);
                
            }
        }

        return $this->render('index',['logs' => $logs,'data'=>$data,'search_words'=>$search_words,'search_files'=>$search_files]);
    }
    
    public function actionMigrateLog()
    {
        $logs_files = [
//             'migrate_member_phone.txt',
//             'migrate_member_phone_1.txt',
//             'migrate_member_phone_2.txt',
            'migrate_member_phone_up.txt',
        ];
        
        $data = [];
        
        foreach ($logs_files as $log_file) {
            
            $filename = $this->logsPath.'migrate/'.$log_file;
            
            if (is_file($filename)) {
                $handle  = fopen ($filename, "r");
                while (!feof ($handle))
                {
                    $buffer  = fgets($handle);
                    if ($buffer) {
                        //查找这个有
                        if (!stripos($buffer, 'data_error_1')) {//data_error_1
                            continue;
                        }
                        
                        $start = stripos($buffer, '"userid":"');
                        $end = strrpos($buffer, '","');
                        
                        if ($start !== false && $end !== false) {
                            $start = $start + 10;
                            $len = $end - $start;
                            $uid = substr($buffer, $start, $len);
                            $data[] = intval($uid);
                        }  
                    }
                }
                fclose ($handle); 
            }   
        }

        $data = array_unique($data);
        
        $data = array_chunk($data, 100);
        
        foreach ($data as $key => $val) {
            
            $str = implode(',', $val);
            
            $str = "'" . ($key+1) . "'=>'" . $str . "',";
            
            file_put_contents('member_phone_up_1.txt', $str. "\r\n", FILE_APPEND);
            
        }
        


        
        
        
        
    }
    
    
    
    


}
